package com.xdcplus.web.service.pm.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xdcplus.mom.enums.ResponseEnum;
import com.xdcplus.mp.service.impl.BaseServiceImpl;
import com.xdcplus.web.common.pojo.entity.pm.PmWo;
import com.xdcplus.web.common.pojo.vo.pm.PmWoVO;
import com.xdcplus.web.mapper.pm.PmWoMapper;
import com.xdcplus.web.service.bd.BdMaterialService;
import com.xdcplus.web.service.bd.BdPathiService;
import com.xdcplus.web.service.pm.PmWoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

/**
 * 工单表(PmWo)表服务实现类
 *
 * @author Fish.Fei
 * @since 2023-02-02 10:26:20
 */
@Slf4j
@Service("pmWoService")
public class PmWoServiceImpl extends BaseServiceImpl<PmWoMapper, PmWo, PmWo, PmWoVO> implements PmWoService {

    @Autowired
    protected PmWoMapper pmWoMapper;

    @Autowired
    protected BdMaterialService bdMaterialService;

    @Autowired
    protected BdPathiService bdPathiService;

    @Override
    public Boolean saveOrUpdateBatch(List<PmWo> pmWoList) {

        pmWoList.forEach(pmWo -> {
            PmWo pmWoParam = new PmWo();
            pmWoParam.setId(pmWo.getId());
            if (ObjectUtil.isNotNull(pmWo.getId())) {
                pmWo.setId(pmWo.getId());
                pmWo.setUpdatedTime(new Timestamp(DateUtil.current()));
                LambdaUpdateWrapper<PmWo> lambdaUpdate = Wrappers.lambdaUpdate();
                lambdaUpdate.eq(PmWo::getId, pmWo.getId());
                update(pmWo, lambdaUpdate);
            } else {
                pmWo.setCreatedTime(new Timestamp(DateUtil.current()));
                save(pmWo);
            }
        });
        return true;
    }

    @Override
    public PmWo getPmWoById(Long id) {
        Assert.notNull(id, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());
        return this.getById(id);
    }

    @Override
    public PmWo getPmWoByWoCode(String woCode) {
        return pmWoMapper.queryByWoCode(woCode);
    }

    @Override
    public PmWoVO getPmWoVoByWoCode(String woCode) {
        return this.objectConversion(pmWoMapper.queryByWoCode(woCode));
    }

    @Override
    public PmWoVO objectConversion(PmWo s) {
        PmWoVO pmWoVO = super.objectConversion(s);
        if (pmWoVO != null) {
            if (pmWoVO.getProductIdFk() != null) {
                pmWoVO.setBdMaterialVO(bdMaterialService.queryBdMaterialById(pmWoVO.getProductIdFk()));
            }
            if (pmWoVO.getPathiIdFk() != null) {
                pmWoVO.setBdPathiVO(bdPathiService.queryBdPathiById(pmWoVO.getPathiIdFk()));
            }
        }

        return pmWoVO;
    }

    @Override
    public List<PmWoVO> objectConversion(List<PmWo> sList) {
        if (!CollectionUtil.isNotEmpty(sList)) {
            return null;
        } else {
            List<PmWoVO> tList = new ArrayList();
            Iterator var3 = sList.iterator();

            while (var3.hasNext()) {
                PmWo s = (PmWo) var3.next();
                PmWoVO t = this.objectConversion(s);
                Optional.ofNullable(t).ifPresent(tList::add);
            }

            return tList;
        }
    }

}
